package exam.uit;

import java.io.IOException;
import java.util.List;
import java.util.Locale;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.MyLocationOverlay;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.drawable.Drawable;
import android.location.Address;
import android.location.Criteria;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.MotionEvent;
import android.widget.Toast;

public class Map extends MapActivity implements LocationListener{
    /** Called when the activity is first created. */
    MapView map;
    
    long start;
    long stop;
    MyLocationOverlay compass;
    MapController controler;
    int x, y;
    GeoPoint touchedPoint;
    Drawable d;
    List<Overlay> overlayList;
    LocationManager lm;
    String towers;
    int lat;
    int longi;
    
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.map);
        
        map = (MapView) findViewById(R.id.mvMain);
        map.setBuiltInZoomControls(true);
        
        Touchy t = new Touchy();
        overlayList = map.getOverlays();
        overlayList.add(t);
        compass = new MyLocationOverlay(Map.this, map);
        overlayList.add(compass);
        controler = map.getController();
        GeoPoint point = new GeoPoint(51643234, 78485931);
        controler.animateTo(point);
        controler.setZoom(6);
        d = getResources().getDrawable(R.drawable.icon);
        
        //Placing pinpoint at Location
        lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
        Criteria crit = new Criteria();
        towers = lm.getBestProvider(crit, false);
        Location location = lm.getLastKnownLocation(towers);
        if(location != null){
        	lat = (int) location.getAccuracy();
        	longi = (int) location.getLongitude();
        	GeoPoint ourLocation = new GeoPoint(lat, longi);
            OverlayItem overlayItem = new OverlayItem(touchedPoint, "What's up", "2nd String");
    		CustomPinpoint custom = new CustomPinpoint(d, Map.this);
    		custom.insertPinpoint(overlayItem);
    		overlayList.add(custom);
        }else{
        	Toast.makeText(Map.this, "Counld't get provider", Toast.LENGTH_SHORT).show();
        }
        
        
        
	}

	
	
	@Override
	protected void onPause() {
		compass.disableCompass();
		super.onPause();
		lm.removeUpdates(this);
	}



	@Override
	protected void onResume() {
		compass.enableCompass();
		super.onResume();
		lm.requestLocationUpdates(towers, 500, 1, this);
	}



	@Override
	protected boolean isRouteDisplayed() {
		return false;
	}
	
	class Touchy extends Overlay{
		public boolean onTouchEvent(MotionEvent e, MapView v){
		if(e.getAction() == MotionEvent.ACTION_DOWN)
		{
			start = e.getEventTime();
			x = (int) e.getX();
			y = (int) e.getY();
			touchedPoint = map.getProjection().fromPixels(x,y);
		}
		if(e.getAction() == MotionEvent.ACTION_UP){
			stop = e.getEventTime();
		}
		if(stop - start > 1500)
		{
			AlertDialog alert = new AlertDialog.Builder(Map.this).create();
			alert.setTitle("Pic a option");
			alert.setMessage("I told u to pick a option");
			alert.setButton("place a pin",new DialogInterface.OnClickListener() {
				
				public void onClick(DialogInterface dialog, int which) {
					
				}
			});
			
			alert.setButton2("get adress",new DialogInterface.OnClickListener() {
				
				public void onClick(DialogInterface dialog, int which) {
					Geocoder geocoder = new Geocoder(getBaseContext(),Locale.getDefault());
					try{
						List<Address> address = geocoder.getFromLocation(touchedPoint.getLatitudeE6()/1E6, touchedPoint.getLongitudeE6()/1E6, 1);
						if(address.size() > 0){
							for(int i=0;i<address.get(0).getMaxAddressLineIndex();i++){
								String display ="";
								display += address.get(0).getAddressLine(i) + "\n";
								Toast t3 = Toast.makeText(getBaseContext(), display, Toast.LENGTH_LONG);
								t3.show();
							}
						}
						
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}finally{
						
					}
					
				}
			});
			alert.setButton3("Toggle View",new DialogInterface.OnClickListener() {
				//chuyen cho do xem 
				public void onClick(DialogInterface dialog, int which) {
					if(map.isSatellite()){
						map.setSatellite(false);
						map.setStreetView(true);
					}else{
						map.setSatellite(true);
						map.setStreetView(false);
					}
					
				}
			});
			alert.show();
			return true;
			
		}
		return false;
	}
	}

	public void onLocationChanged(Location l) {
		lat = (int) (l.getLatitude() * 1E6);
		lat = (int) (l.getLongitude()* 1E6);
		GeoPoint ourLocation = new GeoPoint(lat, longi);
        OverlayItem overlayItem = new OverlayItem(touchedPoint, "What's up", "2nd String");
		CustomPinpoint custom = new CustomPinpoint(d, Map.this);
		custom.insertPinpoint(overlayItem);
		overlayList.add(custom);
	}



	public void onProviderDisabled(String provider) {
		// TODO Auto-generated method stub
		
	}



	public void onProviderEnabled(String provider) {
		// TODO Auto-generated method stub
		
	}



	public void onStatusChanged(String provider, int status, Bundle extras) {
		// TODO Auto-generated method stub
		
	}
	
	
}